package com.led_on_off.led;

import com.led_on_off.led.slice.DeviceListSlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;

public class DeviceList extends Ability {
    public static final String EXTRA_ADDRESS = "device_address" ;
    public static final String Use_BLUETOOTH = "ohos.permission.USE_BLUETOOTH";
    public static final String DISCOVER_BLUETOOTH = "ohos.permission.DISCOVER_BLUETOOTH";
    public static final String LOCATION =  "ohos.permission.LOCATION";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        if (verifySelfPermission(Use_BLUETOOTH) != IBundleManager.PERMISSION_GRANTED) {
            requestPermissionsFromUser(new String[]{Use_BLUETOOTH},0);
        }
        if (verifySelfPermission(DISCOVER_BLUETOOTH) != IBundleManager.PERMISSION_GRANTED) {
            onRequestPermissionsFromUserResult(1,new String[]{DISCOVER_BLUETOOTH},new int[]{});
        }
        if (verifySelfPermission(LOCATION) != IBundleManager.PERMISSION_GRANTED) {
            onRequestPermissionsFromUserResult(2,new String[]{LOCATION},new int[]{});
        }


        super.setMainRoute(DeviceListSlice.class.getName());
    }

    @Override
    public void onRequestPermissionsFromUserResult (int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // 匹配requestPermissions的requestCode
                if (grantResults.length > 0
                        && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                    // 权限被授予
                    // 注意：因时间差导致接口权限检查时有无权限，所以对那些因无权限而抛异常的接口进行异常捕获处理
                    String s  = "";
                } else {
                    // 权限被拒绝
                    String s  = "";
                }
                return;
            }
        }
    }
}
